// * 使用 class-validator 进行验证 ,然后去 main.ts 进行 app.useGlobalPipes(new ValidationPipe()) 注册 or 自己实现 pipe

import { IsString, IsNotEmpty, Length, ValidationOptions } from "class-validator";

export class ValidateCommonDto {
  @IsNotEmpty({ message: "用户名不能为空" })
  @IsString()
  @Length(3, 16, {
    message: "用户名的长度最小为3,最大为16",
  })
  nickname: string;
}

function IsPassword(params: { text: string; min?: number; max?: number }, validationOptions?: ValidationOptions) {
  const { text, min = 6, max = 16 } = params;
  return function (classObj: any, propertyName: string) {
    IsNotEmpty({ message: `${text}不能为空` })(classObj, propertyName);
    IsString()(classObj, propertyName);
    Length(min, max, { message: `${text}的长度最小为${min},最大为${max}` })(classObj, propertyName);
  };
}

export class ValidateAccountDto extends ValidateCommonDto {
  // @IsNotEmpty({ message: "密码不能为空" })
  // @IsString()
  // @Length(6, 16, {
  //   message: "密码的长度最小为6,最大为16",
  // })
  @IsPassword({ text: "密码" })
  password: string;
}

export class ValidatePasswordDto extends ValidateCommonDto {
  // @IsNotEmpty({ message: "新密码不能为空" })
  // @IsString()
  // @Length(6, 16, {
  //   message: "新密码的长度最小为6,最大为16",
  // })
  @IsPassword({ text: "新密码" })
  newPassword: string;

  // @IsNotEmpty({ message: "旧密码不能为空" })
  // @IsString()
  // @Length(6, 16, {
  //   message: "旧密码的长度最小为6,最大为16",
  // })
  @IsPassword({ text: "旧密码" })
  oldPassword: string;
}
